<template>
  <BasicModal v-bind="$attrs" @ok="handleOk" @register="registerModal">
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>

<script setup lang="ts">
  import { modalFormSchema } from './data';
  import mcApi from 'mc-c2b2m-operator-api';
  import { useMessage } from '@/hooks/web/useMessage';
  import { BasicForm, useForm } from '@/components/Form';
  import { BasicModal, useModalInner } from '@/components/Modal';

  const { SystemModule } = mcApi;
  const { UserApi } = SystemModule;

  const [registerForm, { setFieldsValue, resetFields, validate }] = useForm({
    labelWidth: 100,
    schemas: modalFormSchema,
    baseColProps: { span: 22 },
    showActionButtonGroup: false,
  });

  const [registerModal, { setModalProps }] = useModalInner(async (data: any) => {
    await resetFields();
    setModalProps({
      minHeight: 50,
      confirmLoading: false,
      title: `指定推荐人`,
    });
    const { id, referrerUserId } = data.record || {};
    await setFieldsValue({
      referrerUserId,
      referralUserId: id,
    });
  });

  const { createMessage } = useMessage();
  const emits = defineEmits(['success']);
  async function handleOk() {
    try {
      const values: any = await validate();
      setModalProps({ confirmLoading: true });
      await UserApi.appointReferralRel(values);
      createMessage.success(`指定推荐人成功！`);
      emits('success');
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>

<style scoped lang="less"></style>
